package com.dfk.bs.controller.user;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dfk.bs.common.vo.R;
import com.dfk.bs.entity.User;
import com.dfk.bs.entity.admin.Admin;
import com.dfk.bs.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@Controller
@RequestMapping("/user")
public class UserLoginController {

    @Autowired
    UserService userService;

    /**
     * 登录
     */
    @ApiOperation("登录接口")
    @ResponseBody
    @PostMapping("/login")
    public R login( @RequestBody JSONObject json) {
        String username = json.getString("username");
        String password = json.getString("password");

        User one = userService.getOne(
                Wrappers.<User>lambdaQuery()
                        .eq(User::getUsername, username)
                        .eq(User::getPassword, password), false
        );
        if (one==null){
            return R.error().message("用户名或密码错误");
        }
        return R.ok().message("登陆成功").data("user",one);
    }


    /**
     * 登录
     */
    @ApiOperation("退出登录接口")
    @ResponseBody
    @PostMapping("/logout")
    public R logout() {
        return R.ok().message("退出成功");
    }

}
